package org.cwnuit.course.service;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.cwnuit.course.dao.ClassesDAO;
import org.cwnuit.course.domain.Classes;
import org.cwnuit.course.domain.Coursebox;

/**
 * 班级信息表Service
 * @author john
 */
public class ClassesService {
	private ClassesDAO classesDAO =  new ClassesDAO();
	/**
	 * 通过班级ID获取班级信息
	 * @param classid
	 */
	public Classes getClasses(Integer classid){
		return classesDAO.getClasses(classid);
	}
	
	/**
	 * 获取用所有班级信息
	 * @return
	 */
	public List<Classes> listClasses(){
		return classesDAO.listClasses();
	}
	
	/**
	 * 添加或修改班级信息
	 * @param request
	 */
	public boolean saveClasses(HttpServletRequest request) {
		String classidStr = request.getParameter("classid")==""?"0":request.getParameter("classid");
		int classid = Integer.parseInt(classidStr);
		Coursebox coursebox = (Coursebox)request.getSession().getAttribute("coursebox");
		int courseid = coursebox.getCourseid();
		String classname = request.getParameter("classname");
		int snum = Integer.parseInt(request.getParameter("snum"));
		int isused = Integer.parseInt(request.getParameter("isused"));
		int roomid = Integer.parseInt(request.getParameter("roomid"));
		String remarks = request.getParameter("remarks");
		
		Classes classes = new Classes();
		classes.setCourseid(courseid);
		classes.setClassid(classid);
		classes.setClassname(classname);
		classes.setSnum(snum);
		classes.setIsused(isused);
		classes.setRoomid(roomid);
		classes.setRemarks(remarks);
		try{
			if(classid != 0){
				classesDAO.updateClasses(classes);
			}else{
				classesDAO.insertClasses(classes);
			}
			return true;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}
	
	/**
	 * 删除班级信息
	 * @param classid
	 */
	public boolean deleteClasses(Integer classid){
		try{
			Classes classes = new Classes();
			classes.setClassid(classid);
			classesDAO.deleteClasses(classes);
			return true;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
		
	}
}
